package com.panshi.util;

import com.panshi.result.R;
import com.panshi.service.impl.MailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class  UserRegMailUtils {

    @Autowired
    private MailService mailService;

    @Autowired
    private RedisUtil redisUtil;

    public synchronized R regMail(String emil,String form) {
        Object o = redisUtil.get(emil+"_"+form);
        R r = new R();
        if(o!=null && (o+"").length() == 6){
            r.setCode(202);
            r.setSuccess(true);
            r.setMessage("验证码未失效");
            return r;
        }
        String nonce_str = DxyzmUtil.getNonce_str(6);
        mailService.sendMail(nonce_str,emil,form);
        redisUtil.set(emil+"_"+form,nonce_str,90);   //值保存90秒
        r.setCode(201);
        r.setSuccess(true);
        r.setMessage("发送成功");
        return r;
    }
}
